package com.fhz.kotlinstest.paging

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fhz.kotlinstest.R

class PagingActivity : AppCompatActivity() {

    val handler:Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_paging)

        val myRecyclerView: RecyclerView = findViewById(R.id.my_recycler_view)

        val list = mutableListOf<Student>()
        repeat(3){
            list.add(Student("小明","https://th.bing.com/th/id/R.c6fc977ab1effc2f57c1b61ea0b4a10b?rik=xOkA2BAHTYBnfA&riu=http%3a%2f%2fpic.3gbizhi.com%2f2016%2f0619%2f20160619103137648.jpg&ehk=NulKD8j21Eu6wB9zuAYpgZ8H3J7vxPq8IiYqI%2bx%2fRy8%3d&risl=&pid=ImgRaw&r=0"))

        }
//        val myAdapter = MyAdapter(list)
        val myAdapter = UserAdapter()
        myRecyclerView.adapter = myAdapter
        myRecyclerView.setHasFixedSize(true)
        myRecyclerView.setRecycledViewPool(myRecyclerView.recycledViewPool)
        myRecyclerView.layoutManager = LinearLayoutManager(this)
        myAdapter.setData(list)


        handler.postDelayed({
            //同步
//            val newList = mutableListOf<Student>()
//            newList.addAll(list)
//            newList.set(1,Student("小红","https://th.bing.com/th/id/R.c6fc977ab1effc2f57c1b61ea0b4a10b?rik=xOkA2BAHTYBnfA&riu=http%3a%2f%2fpic.3gbizhi.com%2f2016%2f0619%2f20160619103137648.jpg&ehk=NulKD8j21Eu6wB9zuAYpgZ8H3J7vxPq8IiYqI%2bx%2fRy8%3d&risl=&pid=ImgRaw&r=0"))
//
//            val diffResult  = DiffUtil.calculateDiff(MyCallback(list,newList))
//            diffResult.dispatchUpdatesTo(myAdapter)

            //异步
//            val newList = mutableListOf<Student>()
//                val userBean1 = Student("小红","https://th.bing.com/th/id/R.c6fc977ab1effc2f57c1b61ea0b4a10b?rik=xOkA2BAHTYBnfA&riu=http%3a%2f%2fpic.3gbizhi.com%2f2016%2f0619%2f20160619103137648.jpg&ehk=NulKD8j21Eu6wB9zuAYpgZ8H3J7vxPq8IiYqI%2bx%2fRy8%3d&risl=&pid=ImgRaw&r=0")
//                val userBean2 = Student("小明","https://th.bing.com/th/id/R.c6fc977ab1effc2f57c1b61ea0b4a10b?rik=xOkA2BAHTYBnfA&riu=http%3a%2f%2fpic.3gbizhi.com%2f2016%2f0619%2f20160619103137648.jpg&ehk=NulKD8j21Eu6wB9zuAYpgZ8H3J7vxPq8IiYqI%2bx%2fRy8%3d&risl=&pid=ImgRaw&r=0")
//                val userBean3 = Student("小明","https://th.bing.com/th/id/R.c6fc977ab1effc2f57c1b61ea0b4a10b?rik=xOkA2BAHTYBnfA&riu=http%3a%2f%2fpic.3gbizhi.com%2f2016%2f0619%2f20160619103137648.jpg&ehk=NulKD8j21Eu6wB9zuAYpgZ8H3J7vxPq8IiYqI%2bx%2fRy8%3d&risl=&pid=ImgRaw&r=0")
//                newList.add(userBean1)
//                newList.add(userBean2)
//                newList.add(userBean3)
//            myAdapter.setData(newList)

            //局部
            (myRecyclerView.findViewHolderForAdapterPosition(2) as? RecyclerView.ViewHolder)?.let { holder ->
                // 使用payload进行局部刷新
                myAdapter.notifyItemRangeChanged(2, 1, "大名")
            }

        },2000)



    }
}